package part3.c16_singleton.monostate_demo;

/**
 *  假定本类是一个monostate。
 *  期望可以向turnsTitle实例发送事件，并从不同的实例中，查询到结果。
 *
 *  @see #deposit() 存款
 *  @see #refund() 退款
 *  可以注意到，派生的2个子类，依然访问的是相同的变量和方法
 *  @see Locked
 *  @see UnLocked
 *
 */
public class Turnstitle {
    private static boolean isLocked = true;
    private static boolean isAlarming = false;
    private static int itsCoins = 0;
    private static int itsRefunds = 0;
    protected final static Turnstitle LOCKED = new Locked();
    protected final static Turnstitle UNLOCKED = new UnLocked();
    protected static Turnstitle itsState = LOCKED;

    public void reset(){
        lock(true);
        alarm(false);
        itsCoins = 0;
        itsRefunds = 0;
        itsState = LOCKED;
    }
    //getter setter
    public boolean locked(){
        return isLocked;
    }
    public void lock(boolean shouldLock){
        isLocked = shouldLock;
    }
    public boolean alarm(){
        return isAlarming;
    }
    public void alarm(boolean shouldAlarm){
        isAlarming = shouldAlarm;
    }
    public int refunds(){
        return itsRefunds;
    }
    public int coins(){
        return itsCoins;
    }

    //change static
    public void deposit(){
        itsCoins++;
    }
    public void refund(){
        itsRefunds++;
    }

    //public
    public void coin(){
        itsState.coin();
    }
    public void pass(){
        itsState.pass();
    }
}

class Locked extends Turnstitle{
    public void coin(){
        itsState = UNLOCKED;
        lock(false);
        alarm(false);
        deposit();
    }

    public void pass(){
        alarm(true);
    }
}
class UnLocked extends Turnstitle{
    public void coin(){
        refund();
    }
    public void pass(){
        lock(true);
        itsState = LOCKED;
    }
}
